package com.edmond.controller;

import com.edmond.entity.Category;
import com.edmond.service.ICategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/category")
public class AdminCategoryController {
    //  注入业务层对象
    @Resource
    private ICategoryService categoryService;

    //  跳转到显示商品列表的页面
    @RequestMapping("/list")
    public String list(Model model) {
        List<Category> categories = categoryService.list();
        //  将查询的结果封装进model对象
        model.addAttribute("categoryList", categories);
        return "/admin/category_list";
    }

    //  显示添加信息详情页面
    @RequestMapping("/add_show")
    public String add_show() {
        return "/admin/category_add";
    }

    //  添加商品类别信息
    @RequestMapping("/add_submit")
    public String add_submit(Category category) {
        categoryService.insert(category);
        return "redirect:/admin/category/list";
    }

    //  显示修改页面
    @RequestMapping("/update_show")
    public String update_show(Integer categoryId, Model model) {
        Category category = categoryService.find(categoryId);
        model.addAttribute("category", category);
        return "/admin/category_update";
    }

    //  执行更新的业务接口
    @RequestMapping("/update")
    public String update(Category category) {
        categoryService.update(category);
        return "redirect:/admin/category/list";
    }
}
